package gui.parta;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;

/**
 * 
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class Q2 extends JFrame {
    public static void main(String[] args) {
        MultiDataPacket multiDataPacket = new MultiDataPacket();
        Q2 q2 = new Q2(multiDataPacket);
        q2.setResizable(false);
        q2.setBounds(20, 20, 380, 650);
        q2.setTitle("Data Package Convert");
    }

    // 布局组件
    JLabel b1, b2, b3, b5, b7, b9;
    JTextField b4, b6, b8, b10;
    JButton b11, b12;

    public Q2(MultiDataPacket multiDataPacket) {
        init(multiDataPacket);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void init(MultiDataPacket multiDataPacket) {
        setLayout(null);
        JPanel panel = new JPanel();
        panel.setBounds(18, 18, 330, 570);
        panel.setLayout(new GridLayout(6, 2, 15, 15));

        String strMsg = "<html><body>" + "Name:Shuyue Si"
                + "<br>" + "Gender:Male"
                + "<br>" + "StuID:2021" +
                "<body></html>";
        b1 = new JLabel(strMsg);
        b1.setForeground(Color.red);
        // 插入图片
        b2 = new JLabel();
        // System.out.println(Q2.class.getClassLoader().getResource(""));
        InputStream resourceAsStream = Q2.class.getClassLoader().getResourceAsStream("gui/parta/aa.png");
        // if (image != null) {
        //
        // }
        ImageIcon imageIcon = new ImageIcon("gui/parta/aa.png");
        b2.setIcon(imageIcon);
        b2.setHorizontalTextPosition(JLabel.CENTER);
        b3 = new JLabel("Raw data 1:");
        b4 = new JTextField();
        b5 = new JLabel("Raw data 2:");
        b6 = new JTextField();
        b7 = new JLabel("Raw data 3:");
        b8 = new JTextField();
        b9 = new JLabel("Multi data package:");
        b10 = new JTextField();
        b11 = new JButton("Data to multi package");
        b11.addActionListener(new MultiMonitor(this, multiDataPacket));
        b12 = new JButton("Multi package to data");
        b12.addActionListener(new MultiMonitor(this, multiDataPacket));
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        panel.add(b5);
        panel.add(b6);
        panel.add(b7);
        panel.add(b8);
        panel.add(b9);
        panel.add(b10);
        panel.add(b11);
        panel.add(b12);
        add(panel, BorderLayout.CENTER);
    }
}

class MultiMonitor implements ActionListener {
    Q2 tf;
    MultiDataPacket multiDataPacket;

    public MultiMonitor(Q2 tf, MultiDataPacket multiDataPacket) {
        this.tf = tf;
        this.multiDataPacket = multiDataPacket;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if ("Data to multi package".equals(actionCommand)) {
            String text4 = tf.b4.getText();
            String text6 = tf.b6.getText();
            String text8 = tf.b8.getText();
            String[] strings = MultiDataPacket.stringToHex(new String[]{text4, text6, text8});
            StringBuilder res = new StringBuilder();
            for (String string : strings) {
                res.append(string).append(",");
            }
            tf.b10.setText(res.toString());
        }
        if ("Multi package to data".equals(actionCommand)) {
            String text = tf.b10.getText();
            String[] split = text.split(",");
            for (int i = 0; i < split.length; i++) {
                if (i == 0) {
                    String v1 = DataPacket.getDataFromDataPacket(split[0]);
                    tf.b4.setText(v1);
                }
                if (i == 1) {
                    String v2 = DataPacket.getDataFromDataPacket(split[1]);
                    tf.b6.setText(v2);
                }
                if (i == 2) {
                    String v3 = DataPacket.getDataFromDataPacket(split[2]);
                    tf.b8.setText(v3);
                }
            }
        }
    }
}
